<?php
/**
 * class mère du CMS, déclare les variables de base
 * @package PWE
 * @author Gmax <gmaxfr78@gmail.com>
 */
class PWE{
    private static $_siteName = 'PWE';
    private static $_baseURL = 'localhost';
    private static $_codeAnalytics = '';
    private static $_origineCategory = 'accueil';
    private static $_homePage = 'accueil.html';
    private static $_home = null;
    private static $_baseDirectory = '';
    private static $_theme = 'default';
    private static $_theme_ui = 'smoothness';
    private static $_logo_media_id = 0;
    private static $_langues = array();
    private static $_mailjet = array();
    private static $_msg;
    private static $_defaultTemplate = '';
    private static $_defaultTitle = '';
    private static $_defaultDescription = '';
    private static $_missingMedia = '';
    private static $_prefixBdd = '';
    private static $_nameAdmin = '';
    private static $_mailAdmin = '';
    private static $_nameContact = '';
    private static $_mailContact = '';
    private static $_nameSupport = '';
    private static $_mailSupport = '';
    private static $_nameService = '';
    private static $_mailService = '';
    private static $_nameNewsletter = '';
    private static $_mailNewsletter = '';
    
    private static $_installMode = false;
    private static $_maintenanceMode = false;
    
    public static $pingSeUrl = array('http://www.google.com/webmasters/tools/ping?sitemap=','http://www.bing.com/webmaster/ping.aspx?siteMap=','http://submissions.ask.com/ping?sitemap=','http://api.moreover.com/ping?sitemap=');
            
    function __construct(){
        session::start();
        
        /* recuperation du fichier de config - chargement de config_default.ini par défaut*/
        if(DEBUG_MODE && file_exists(CONFIG_PATH.'config_local.ini')){
            $configFile = CONFIG_PATH.'config_local.ini';
        }elseif(file_exists(CONFIG_PATH.'config.ini')){
            $configFile = CONFIG_PATH.'config.ini';
        }else{
            self::$_installMode = true;
            $configFile = CONFIG_PATH.'config_tmp.ini';
        }
        
        if(isset($configFile) && file_exists($configFile)){
            /* attribution des valeurs de config */
            $conf = parse_ini_file($configFile,true);
            self::$_siteName = (isset($conf['domaine']['siteName']))?$conf['domaine']['siteName']:'';
            self::$_maintenanceMode = (isset($conf['mode'])&&isset($conf['mode']['maintenance'])&&$conf['mode']['maintenance']==1)?true:false;
            self::$_codeAnalytics = $conf['google']['code_analytics'];
            self::$_baseURL = (substr($conf['domaine']['baseURL'],-1)!=='/')?$conf['domaine']['baseURL'].'/':$conf['domaine']['baseURL'];
            self::$_baseDirectory = ($conf['domaine']['baseDirectory']!==''&&substr($conf['domaine']['baseDirectory'],-1)!=='/')?$conf['domaine']['baseDirectory'].'/':$conf['domaine']['baseDirectory'];
            self::$_origineCategory = $conf['structure']['origine_category'];
            self::$_homePage = $conf['structure']['home_page'];
            self::$_theme = (is_dir(THEME_PATH.$conf['design']['theme']))?$conf['design']['theme']:'default';
            self::$_theme_ui = $conf['design']['theme_ui'];
            self::$_logo_media_id = (isset($conf['design']['logo_media_id']))?$conf['design']['logo_media_id']:0;
            self::$_defaultTemplate = $conf['structure']['default_template'];
            self::$_missingMedia = $conf['design']['missing_media'];
            self::$_nameAdmin = $conf['admin']['name'];
            self::$_mailAdmin = $conf['admin']['mail'];
            self::$_nameContact = $conf['contact']['name'];
            self::$_mailContact = $conf['contact']['mail'];
            self::$_nameSupport = $conf['support']['name'];
            self::$_mailSupport = $conf['support']['mail'];
            self::$_nameService = $conf['service']['name'];
            self::$_mailService = $conf['service']['mail'];
            
            if(isset($conf['langues'])&&count($conf['langues'])>0){
                self::$_langues = $conf['langues'];
            }
            if(isset($conf['mailjet'])&&count($conf['mailjet'])>0){
                self::$_mailjet = array('apikey'=>$conf['mailjet']['apikey'],'secretkey'=>$conf['mailjet']['secretkey']);
                self::$_nameNewsletter = $conf['mailjet']['nom'];
                self::$_mailNewsletter = $conf['mailjet']['mail'];
            }
            
            self::$_prefixBdd = $conf['bdd']['database_prefix'];
            
            /* initialisation du gestionnaire de base de données */
            $sqlConf = $conf['bdd'];
            bdd_sql::setServer($sqlConf);
        }else{
            self::$_installMode = true;
            // passer en mode setup
        }
    }
    /**
     * met à jour le préfix de table de la base de données
     * @param string $newPrefix
     */
    public static function updatePrefixTable($newPrefix){
        self::$_prefixBdd = $newPrefix;
    }
    
    /**
     * traitement post-process du retour 
     * @param string $buffer
     * @return string
     */
    protected function _process($buffer){
        // traitement pattern/short codes
        return $buffer;
    }
    /**
     * indique si le PWE est en mode installation
     * @return boolean
     */
    public static function isInstallMode(){
        return self::$_installMode;
    }
    /**
     * indique si le PWE est en mode maintenace
     * @return boolean
     */
    public static function isMaintenanceMode(){
        return (modele_user::isConnectedAdmin())?false:self::$_maintenanceMode;
    }
    /**
     * renvoi le contenu de page
     */
    public function getContent(){
        // récupération du contenu en fonction du template et des variables d'environnement
        // méthode étendu par chaque controler
    }
    
    /**
     * renvoi le logo du site
     * @return \modele_media
     */
    public function getLogo(){
        $logo = null;
        if(self::$_logo_media_id>0){
            $media = new modele_media(self::$_logo_media_id);
            if($media->getWidth()>0 && $media->getHeight()){
                $logo = $media;
            }
        }
        return $logo;        
    }
     public static function getLogoId(){
         return self::$_logo_media_id;
     }
    // gestion du message interne de retour
    /**
     * @return string
     */
    public static function getMsg(){
        return self::$_msg;
    }
    /**
     * @param string $msg
     */
    public static function setMsg($msg){
        self::$_msg = $msg;
    }
    
    /**
     * @return string
     */
    public function getTitle(){
        return self::$_defaultTitle;
    }
    /**
     * @return string
     */
    public function getDescription(){
        return self::$_defaultDescription;
    }
    /**
     * @return string
     */
    public static function getSiteName(){
        return self::$_siteName;
    }
    /**
     * @return string
     */
    public static function getBaseUrl(){
        return self::$_baseURL.self::getBaseDirectory();
    }
    /**
     * renvoi le permalien de la categorie origine
     * @return string
     */
    public static function getOrigineCategoryName(){
        return self::$_origineCategory;
    }
    /**
    * renvoi l'objet de la categorie origine
    * @return \modele_category
    */
    public static function getOrigineCategory(){
        return modele::findByPermalien(self::getOrigineCategoryName(),'category');
    }
    /**
     * renvoi le permalien de la page d'accueil
     * @return string
     */
    public static function getHomePageName(){
        return self::$_homePage;
    }
    /**
     * renvoi l'objet de la page d'accueil
     * @return \modele_page
     */
    public static function getHomePage(){
        if(self::$_home===null){
            self::$_home = modele::findByPermalien(self::getHomePageName(),'page');
        }
        return self::$_home;
    }
    /**
     * @return string
     */
    public static function getDefaultTemplate(){
        return self::$_defaultTemplate;
    }
    /**
     * @return string
     */
    public static function getMissingMedia(){
        if(file_exists(self::getThemePath().'/'.self::$_missingMedia)){
            return self::getThemePath().'/'.self::$_missingMedia;
        }
        return self::$_missingMedia;
    }
    /**
     * @return string
     */
    public static function getBaseDirectory(){
        return self::$_baseDirectory;
    }
    /**
     * 
     * @return string
     */
    public static function getPrefixBdd(){
        return self::$_prefixBdd;
    }
    /**
     * renvoi le nom du theme de site
     * @return string
     */
    public static function getTheme(){
        return self::$_theme;
    }
    /**
     * renvoi le nom du theme jquery ui
     * @return string
     */
    public static function getThemeUi(){
        return self::$_theme_ui;
    }
    /**
     * renvoi le chemin vers le theme actif
     * @return string
     */
    public static function getThemePath(){
        return THEME_PATH.self::$_theme;
    }
    /**
     * renvoi l'url du theme actif
     * @return string
     */
    public static function getThemeURL(){
        $baseUrl = (substr(self::getBaseUrl(), -1)==='/')?self::getBaseUrl():self::getBaseUrl().'/';
        return $baseUrl.self::getThemePath().'/';
    }
    /**
     * renvoi la liste des langues actives
     * @return array
     */
    public static function getLanguesActives(){
        $retour = array();
        foreach(self::$_langues as $lang=>$statut){
            if($statut==1){
                $retour[] = $lang;
            }
        }
        return $retour;
    }
    /**
     * renvoi le nombre de langues actives
     * @return int
     */
    public static function getNbrLanguesActives(){
        return count(self::getLanguesActives());
    }
    
    /**
     * renvoi la liste des langues
     * @return array
     */
    public static function getLangues(){
        return self::$_langues;
    }
    /**
     * renvoi un tableau de configuration de mailjet [apikey, secretkey]
     * @return array
     */
    public static function getMailjet(){
        return self::$_mailjet;
    }
    /**
     * renvoi le code analytics fourni dans config.ini
     * @return string
     */
    public static function getCodeAnalytics(){
        return self::$_codeAnalytics;
    }
    /**
     * @return string
     */
    public static function getMailAdmin(){
        return self::$_mailAdmin;
    }
    /**
     * @return string
     */
    public static function getNameAdmin(){
        return self::$_nameAdmin;
    }
    /**
     * @return string
     */
    public static function getMailContact(){
        return self::$_mailContact;
    }
    /**
     * @return string
     */
    public static function getNameContact(){
        return self::$_nameContact;
    }
    /**
     * @return string
     */
    public static function getMailSupport(){
        return self::$_mailSupport;
    }
    /**
     * @return string
     */
    public static function getNameSupport(){
        return self::$_nameSupport;
    }
    /**
     * @return string
     */
    public static function getMailService(){
        return self::$_mailService;
    }
    /**
     * @return string
     */
    public static function getNameService(){
        return self::$_nameService;
    }
    /**
     * @return string
     */
    public static function getMailNewsletter(){
        return self::$_mailNewsletter;
    }
    /**
     * @return string
     */
    public static function getNameNewsletter(){
        return self::$_nameNewsletter;
    }
}
?>